#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: 算法初体验_冒泡排序法.py 
@version:
@time: 2023/01/05 
"""
# pip install matplotlib
import matplotlib.pyplot as plt
from random import shuffle


def bubblesort_anim(a):
    x = range(len(a))
    swapped = True  # 标记排序是否已经结束
    while swapped:
        plt.clf()  # 清空显示窗口仪表实现动画效果
        swapped = False
        for i in range(len(a) - 1):
            if a[i] > a[i + 1]:  # 比价相邻两个元素
                a[i + 1], a[i] = a[i], a[i + 1]  # 如果左边元素比右边元素值大，则进行互换
                swapped = True
        plt.plot(x, a, 'k.', markersize=6)  # 将一次冒泡排序后的元素排列变化显示到窗口
        plt.pause(0.01)


a = list(range(100))  # 初始化0~299
shuffle(a)  # 将300个元素随机排列
print(a)  # 执行冒泡排序将300个元素进行升序排列

# bubblesort_anim(a)

def bb(nums):
    flag = True
    while flag:
        flag = False
        for i in range(0, len(nums)-1):
            if nums[i]>nums[i+1]:
                nums[i],nums[i+1] = nums[i+1], nums[i]
                flag = True
    return nums

a = list(range(10))  # 初始化0~299
shuffle(a)
print(a)
print(bb(a))